package com.sniff.atp.service.purchase;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import com.sniff.atp.dto.SystemLoginDTO;
import com.sniff.atp.dto.purchase.CreatePurchaseOrderFailDTO;
import com.sniff.atp.dto.purchase.CreatePurchaseOrderItemDTO;
import com.sniff.atp.entity.PurchaseOrder;
import com.sniff.atp.entity.PurchaseOrderItem;
import com.sniff.atp.enums.api.PurchaseAPI;
import com.sniff.atp.mapper.purchase.PurchaseOrderMapper;
import com.sniff.atp.provider.DataSource;
import com.sniff.atp.provider.DataSourceType;
import com.sniff.atp.utils.DateUtil;
import com.sniff.atp.utils.Headers;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.service.purchase
 * @Author: zxy
 * @CreateTime: 2024-01-31 17:41
 * @Description:
 * @Version: 1.0
 */
@Service
public class PurchaseMockService {
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @DataSource(DataSourceType.PURCHASE)
    public Object createPurchaseOrderFail(SystemLoginDTO systemLoginDTO, String purchaseNo) {

        PurchaseOrder purchaseOrderDTO = purchaseOrderMapper.getPurchaseOrderDTO(purchaseNo);

        List<PurchaseOrderItem> purchaseOrderItemDTO = purchaseOrderMapper.getPurchaseOrderItemDTO(purchaseNo);

        CreatePurchaseOrderFailDTO createPurchaseOrderFailDTO = new CreatePurchaseOrderFailDTO();
        createPurchaseOrderFailDTO.setPurchaseId(String.valueOf(purchaseOrderDTO.getPurchaseId()));
        createPurchaseOrderFailDTO.setPurchaseNo(purchaseOrderDTO.getPurchaseNo());
        createPurchaseOrderFailDTO.setPlatformType("alibaba");
        createPurchaseOrderFailDTO.setStatus("fail");
        createPurchaseOrderFailDTO.setErrorCode("500_004");
        createPurchaseOrderFailDTO.setErrorReason("商品库存不足，请核实库存后订购。");

        List<CreatePurchaseOrderItemDTO> createPurchaseOrderItemDTOs = new ArrayList<>();

        purchaseOrderItemDTO.forEach(item -> {
            CreatePurchaseOrderItemDTO createPurchaseOrderItemDTO = new CreatePurchaseOrderItemDTO();
            createPurchaseOrderItemDTO.setPurchaseId(String.valueOf(item.getPurchaseId()));
            createPurchaseOrderItemDTO.setPurchaseItemId(String.valueOf(item.getPurchaseItemId()));
            createPurchaseOrderItemDTO.setOfferId("561485120973");
            createPurchaseOrderItemDTO.setSpecId(item.getProductSku());
            createPurchaseOrderItemDTO.setQuantityStockShortage(2);

            createPurchaseOrderItemDTOs.add(createPurchaseOrderItemDTO);

        });

        createPurchaseOrderFailDTO.setPurchaseItemRetentionQueries(createPurchaseOrderItemDTOs);

        String json = JSON.toJSONString(createPurchaseOrderFailDTO, true);

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.PURCHASE_MOCK_CREATE_OREDER.getApi(), JsonUtil.strToJSONObject(json));

        return JsonUtil.strToJSONObject(result);
    }


    @DataSource(DataSourceType.PURCHASE)
    public Object createPurchaseOrderSuccess(SystemLoginDTO systemLoginDTO, String purchaseNo) {
        JSONObject jsonObject = JsonUtil.readJson("purchase/mockPurchaseCreateOrder");

        PurchaseOrder purchaseOrderDTO = purchaseOrderMapper.getPurchaseOrderDTO(purchaseNo);

//        purchaseOrderDTO.getWareCode();
//        purchaseOrderDTO.getPlatformType();
//        purchaseOrderDTO.getSystemSource();
//       XSCGD24031806001370	AM	1513770467071086592	2
////        1-D2C; 2-B2B',



//        if (purchaseOrderDTO.getSystemSource() == 1){
//            if ("TB".equals(purchaseOrderDTO.getPlatformType()) || "TM".equals(purchaseOrderDTO.getPlatformType())){
//                if (purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
//                    jsonObject.replace("orderAccount", "zxb2024001@163.com");
//
//                } else if (purchaseOrderDTO.getWareCode().equals("1513771090160111616")) {
//                    jsonObject.replace("orderAccount", "zxb2024002@163.com");
//
//                }else {
//
//                    jsonObject.replace("orderAccount", "zxb2024002@163.com");
//                }
//            }else{
//                if(purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
//                    jsonObject.replace("orderAccount", "zxb2024001@163.com");
//
//                } else if (purchaseOrderDTO.getWareCode().equals("1513771090160111616")) {
//                    jsonObject.replace("orderAccount", "杭州ckb:test002");
//
//                }else {
//
//                    jsonObject.replace("orderAccount", "杭州ckb:test005");
//                }
//            }
//
//        }else{
//
//            if ("TB".equals(purchaseOrderDTO.getPlatformType()) || "TM".equals(purchaseOrderDTO.getPlatformType())){
//                if (purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
//                    jsonObject.replace("orderAccount", "zxb2024003@163.com");
//
//                } else if (purchaseOrderDTO.getWareCode().equals("1513771090160111616")) {
//
//                }else {
//                    jsonObject.replace("orderAccount", "zxb2024003@163.com");
//                }
//
//            }else{
//                if (purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
//                    jsonObject.replace("orderAccount", "zxb2024003@163.com");
//
//                } else if (purchaseOrderDTO.getWareCode().equals("1513771090160111616")) {
//
//                }else {
//                    jsonObject.replace("orderAccount", "杭州ckb:test004");
//                }
//            }
//        }




        if(1 == purchaseOrderDTO.getSystemSource() && "AM".equals(purchaseOrderDTO.getPlatformType()) && purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
            jsonObject.replace("orderAccount", ":zxb2024001@163.com");
        }

        if(1 == purchaseOrderDTO.getSystemSource() && "AM".equals(purchaseOrderDTO.getPlatformType()) && purchaseOrderDTO.getWareCode().equals("1513771090160111616")){
            jsonObject.replace("orderAccount", "杭州ckb:test002");
        }

        if(2 == purchaseOrderDTO.getSystemSource() && "AM".equals(purchaseOrderDTO.getPlatformType()) && purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
            jsonObject.replace("orderAccount", "zxb2024003@163.com");
        }

        if(2 == purchaseOrderDTO.getSystemSource() && "AM".equals(purchaseOrderDTO.getPlatformType()) && purchaseOrderDTO.getWareCode().equals("1704005183976673280")){
            jsonObject.replace("orderAccount", "杭州ckb:test004");
        }

        if(1 == purchaseOrderDTO.getSystemSource() && "AM".equals(purchaseOrderDTO.getPlatformType()) && purchaseOrderDTO.getWareCode().equals("1704005183976673280")){
            jsonObject.replace("orderAccount", "杭州ckb:test005");
        }

        if(1 == purchaseOrderDTO.getSystemSource() && ("TB".equals(purchaseOrderDTO.getPlatformType()) || "TM".equals(purchaseOrderDTO.getPlatformType())) && purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
            jsonObject.replace("orderAccount", "zxb2024001@163.com");
        }

        if(1 == purchaseOrderDTO.getSystemSource() && ("TB".equals(purchaseOrderDTO.getPlatformType()) || "TM".equals(purchaseOrderDTO.getPlatformType())) && purchaseOrderDTO.getWareCode().equals("1513771090160111616")){
            jsonObject.replace("orderAccount", "zxb2024002@163.com");
        }

        if(2 == purchaseOrderDTO.getSystemSource() && ("TB".equals(purchaseOrderDTO.getPlatformType()) || "TM".equals(purchaseOrderDTO.getPlatformType())) && purchaseOrderDTO.getWareCode().equals("1513770467071086592")){
            jsonObject.replace("orderAccount", "zxb2024003@163.com");
        }

        if(1 == purchaseOrderDTO.getSystemSource() && ("TB".equals(purchaseOrderDTO.getPlatformType()) || "TM".equals(purchaseOrderDTO.getPlatformType())) && purchaseOrderDTO.getWareCode().equals("1704005183976673280")){
            jsonObject.replace("orderAccount", "zxb2024002@163.com");
        }

        if(2 == purchaseOrderDTO.getSystemSource() && ("TB".equals(purchaseOrderDTO.getPlatformType()) || "TM".equals(purchaseOrderDTO.getPlatformType())) && purchaseOrderDTO.getWareCode().equals("1704005183976673280")){
            jsonObject.replace("orderAccount", "zxb2024003@163.com");
        }



        jsonObject.replace("purchaseId", purchaseOrderDTO.getPurchaseId());
        jsonObject.replace("purchaseNo", purchaseOrderDTO.getPurchaseNo());
        jsonObject.replace("channelOrderNumber", purchaseOrderDTO.getPurchaseNo().replace("XSCGD", "QDDH"));
        jsonObject.replace("orderTime", DateUtil.dateToString(new Date()));

        jsonObject.replace("discountAmount", 10);
        jsonObject.replace("originShippingAmount", 20);

        BigDecimal originProductAmount = BigDecimal.ZERO;
        BigDecimal productAmount = BigDecimal.ZERO;

        jsonObject.replace("shippingAmount", 10);

        JSONArray jsonArray = new JSONArray();
        List<PurchaseOrderItem> purchaseOrderItemDTO = purchaseOrderMapper.getPurchaseOrderItemDTO(purchaseNo);

        for (PurchaseOrderItem item : purchaseOrderItemDTO) {

            BigDecimal price = item.getSellUnitPrice().multiply(new BigDecimal(0.9));
            BigDecimal originPrice = item.getSellUnitPrice().multiply(new BigDecimal(0.9));

            JSONObject purchaseItemRetentionQuerie = new JSONObject();
            purchaseItemRetentionQuerie.put("purchaseId", String.valueOf(item.getPurchaseId()));
            purchaseItemRetentionQuerie.put("purchaseItemId", String.valueOf(item.getPurchaseItemId()));
            purchaseItemRetentionQuerie.put("platformSku", item.getProductSku());
            purchaseItemRetentionQuerie.put("offerId", "561485120973");
            purchaseItemRetentionQuerie.put("specId", item.getProductSku());
            purchaseItemRetentionQuerie.put("subPurchaseOrderId", purchaseOrderDTO.getPurchaseNo().replace("XSCGD", "QDDH"));
            purchaseItemRetentionQuerie.put("price", price);
            purchaseItemRetentionQuerie.put("originPrice", originPrice);
            purchaseItemRetentionQuerie.put("planOrderQuantity", item.getSellQuantity());
            purchaseItemRetentionQuerie.put("actualOrderQuantity", item.getSellQuantity());

            jsonArray.add(purchaseItemRetentionQuerie);
            originProductAmount = originProductAmount.add(originPrice.multiply(new BigDecimal(item.getSellQuantity())));
            productAmount = productAmount.add(price.multiply(new BigDecimal(item.getSellQuantity())));
        };

        jsonObject.replace("originProductAmount", originProductAmount);
        jsonObject.replace("productAmount", productAmount);

        jsonObject.replace("purchaseItemRetentionQueries", jsonArray);


        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.PURCHASE_MOCK_CREATE_OREDER.getApi(), jsonObject);

        return JsonUtil.strToJSONObject(result);
    }

    @DataSource(DataSourceType.PURCHASE)
    public Object placingOrder(SystemLoginDTO systemLoginDTO, String purchaseNo) {

        PurchaseOrder purchaseOrderDTO = purchaseOrderMapper.getPurchaseOrderDTO(purchaseNo);

        HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
        stringStringHashMap.put("purchaseId", String.valueOf(purchaseOrderDTO.getPurchaseId()));
        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doGet(PurchaseAPI.PLACING_ORDER.getApi(), stringStringHashMap);

        return JsonUtil.strToJSONObject(result);
    }

    @DataSource(DataSourceType.PURCHASE)
    public Object negotiated(SystemLoginDTO systemLoginDTO, String purchaseNo) {

        PurchaseOrder purchaseOrderDTO = purchaseOrderMapper.getPurchaseOrderDTO(purchaseNo);

        HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
        stringStringHashMap.put("purchaseId", String.valueOf(purchaseOrderDTO.getPurchaseId()));

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doGet(PurchaseAPI.PURCHASE_MOCK_NEGOTIATED.getApi(), stringStringHashMap);

        return JsonUtil.strToJSONObject(result);
    }

    @DataSource(DataSourceType.PURCHASE)
    public Object negotiatedPay(SystemLoginDTO systemLoginDTO, String purchaseNo) {

        PurchaseOrder purchaseOrderDTO = purchaseOrderMapper.getPurchaseOrderDTO(purchaseNo);

        HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
        stringStringHashMap.put("purchaseId", String.valueOf(purchaseOrderDTO.getPurchaseId()));

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doGet(PurchaseAPI.PURCHASE_MOCK_PAY.getApi(), stringStringHashMap);

        return JsonUtil.strToJSONObject(result);
    }

    @DataSource(DataSourceType.PURCHASE)
    public Object negotiatedLogistics(SystemLoginDTO systemLoginDTO, String purchaseNo) {

        PurchaseOrder purchaseOrderDTO = purchaseOrderMapper.getPurchaseOrderDTO(purchaseNo);

        HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
        stringStringHashMap.put("purchaseId", String.valueOf(purchaseOrderDTO.getPurchaseId()));

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doGet(PurchaseAPI.PURCHASE_MOCK_LOGISTICS.getApi(), stringStringHashMap);

        return JsonUtil.strToJSONObject(result);
    }
}
